const app = getApp()

Component({
  options: {
    pureDataPattern: /^_/
  },

  data: {
    address: '点击选择，请勾选哦~',
    isSubmit: false,
    isSucc: false,
    _staticData: {
      type: 'sell'
    }
  },


  methods: {
    handleAddressTap() {
      wx.chooseLocation({
        success: (res) => {
          let {
            latitude,
            longitude,
            address
          } = res
  
          this.setData({
            address,
            latitude,
            longitude
          })
        }
      })
    },
  
    handleRadioChange (e) {
      this.data._staticData = {
        ...this.data._staticData,
        type: e.detail.value
      }
    },
  
    handleMessageTap(e) {
      this.data._staticData = {
        ...this.data._staticData,
        message: e.detail.value
      }
    },
  
    handleContactTap(e) {
      this.data._staticData = {
        ...this.data._staticData,
        contact: e.detail.value
      }
    },
  
    showInfo(title) {
      wx.showToast({
        title,
        icon: 'none',
        duration: 2000
      })   
    },
  
    handleSubmitTap() {
      if (this.data.address === '点击选择，请勾选哦~') {
        this.showInfo('请选择地址')
        return
      }
  
      if (this.data._staticData.message === '') {
        this.showInfo('请输入说明')
        return
      }
  
      if (this.data._staticData.contact === '') {
        this.showInfo('请输入联系方式')
        return
      }
  
  
      let data = {
        ...this.data._staticData,
        ...this.data
      }
  
      app.minipet.add({
        data,
        success: (result) => {
          this.setData({
            isSucc: true
          })
        },
        fail: (error) => {
          this.setData({
            isSucc: false
          })
        },
        complete: () => {
          this.setData({
            isSubmit: true
          })
        }
      })
    }
  }
})